package org.suanfaTranning.ST200_Chatpter1_LineTable_HashTable_String.hashTable;

/**
 * @Auther: qingle
 * @Date: 2024/9/11-1:12
 * @Description:
 * @version: 1.0
 */
import java.util.LinkedList;
import java.util.Queue;

public class RecentCounter {
	private final Queue<Integer> requests;

	public RecentCounter() {
		requests = new LinkedList<>();
	}

	public int ping(int t) {
		requests.offer(t);
		// 移除不在时间范围内的请求
		while (!requests.isEmpty() && requests.peek() < t - 3000) {
			requests.poll();
		}
		// 返回当前时间范围内的请求数量
		return requests.size();
	}

	public static void main(String[] args) {
		RecentCounter counter = new RecentCounter();
		int[] timestamps = {1, 100, 3001, 3002};
		int[] expectedOutput = {1, 2, 3, 3};

		for (int timestamp : timestamps) {
			int result = counter.ping(timestamp);
			System.out.println(result); // 打印每次 ping 的结果
		}
	}
}